package me.minecraft.woodenman;

import lombok.Getter;
import me.minecraft.woodenman.command.JoinCommand;
import me.minecraft.woodenman.command.MainCommand;
import me.minecraft.woodenman.command.QuitCommand;
import me.minecraft.woodenman.listener.PlayerEvent;
import me.minecraft.woodenman.runnable.GameTask;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * @author langle__
 * @version 1.0
 */
public class WoodenMan extends JavaPlugin {

    @Getter
    private static WoodenMan instance;

    public static GameTask gameTask = null;
    public static Location playerLocation = null;
    public static Location woodenManLocation = null;

    @Override
    public void onEnable() {
        instance = this;
        this.saveDefaultConfig();
        this.reloadConfig();
        this.registerCommand("WoodenMan", new MainCommand());
        this.registerCommand("WoodenManJoin", new JoinCommand());
        this.registerCommand("WoodenManQuit", new QuitCommand());
        Bukkit.getPluginManager().registerEvents(new PlayerEvent(), this);
    }

    @Override
    public void onDisable() {
        saveConfig();
    }

    @Override
    public void reloadConfig() {
        super.reloadConfig();
        loadLocationsFromConfig();
    }

    @Override
    public void saveConfig() {
        FileConfiguration config = getConfig();
        World world;
        if (playerLocation != null) {
            world = playerLocation.getWorld();
        } else if (woodenManLocation != null) {
            world = woodenManLocation.getWorld();
        } else {
            return;
        }
        if (world == null) {
            this.getLogger().warning("World not found in config.yml");
        } else {
            config.set("location.world", world.getName());
        }
        if (playerLocation != null) {
            config.set("location.player.x", playerLocation.getBlockX());
            config.set("location.player.y", playerLocation.getBlockY());
            config.set("location.player.z", playerLocation.getBlockZ());
        }
        if (woodenManLocation != null) {
            config.set("location.woodenMan.x", woodenManLocation.getBlockX());
            config.set("location.woodenMan.y", woodenManLocation.getBlockY());
            config.set("location.woodenMan.z", woodenManLocation.getBlockZ());
        }
        super.saveConfig();
    }

    public void registerCommand(String commandName, CommandExecutor executor) {
        PluginCommand command = this.getCommand(commandName);
        if (command == null) {
            this.getLogger().warning("Command '" + commandName + "' not found in plugin.yml");
            return;
        }
        command.setExecutor(executor);
    }

    /**
     * 从配置文件中加载玩家和木人的位置信息。
     * 如果配置不完整或无效，将记录警告日志并跳过相关设置。
     */
    private void loadLocationsFromConfig() {
        FileConfiguration config = this.getConfig();
        // 获取 worldName 并判断是否存在
        String worldName = config.getString("location.world");
        if (worldName == null) return;

        World world = Bukkit.getWorld(worldName);
        if (world == null) {
            this.getLogger().warning("World '" + worldName + "' not found in config.yml");
            return;
        }

        // 获取 player 的坐标并判断是否存在
        if (config.contains("location.player.x") &&
                config.contains("location.player.y") &&
                config.contains("location.player.z")
        ) {
            int playerX = config.getInt("location.player.x");
            int playerY = config.getInt("location.player.y");
            int playerZ = config.getInt("location.player.z");
            playerLocation = new Location(world, playerX, playerY, playerZ);
        }

        // 获取 woodenMan 的坐标并判断是否存在
        if (config.contains("location.woodenMan.x") &&
                config.contains("location.woodenMan.y") &&
                config.contains("location.woodenMan.z")
        ) {
            int woodenManX = config.getInt("location.woodenMan.x");
            int woodenManY = config.getInt("location.woodenMan.y");
            int woodenManZ = config.getInt("location.woodenMan.z");
            woodenManLocation = new Location(world, woodenManX, woodenManY, woodenManZ);
        }
    }

    /**
     * 判断玩家是否在游戏内，并且游戏是否为 null。
     *
     * @param player 要判断的玩家
     * @return 如果游戏不为 null 且玩家在游戏内，返回 true；否则返回 false
     */
    public static boolean isPlayerInGame(Player player) {
        if (gameTask == null) {
            return false;
        }
        return gameTask.isPlayerInGame(player);
    }

}
